home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 2701 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  98 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: EU.net!sun4nl!hguijt
  3. From: hguijt@inter.NL.net (Hans Guijt)
  4. Subject: Re: silly c problem
  5. Message-ID: <DM8sCu.Huy@inter.NL.net>
  6. Organization: NLnet
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. Date: Sun, 4 Feb 1996 08:17:18 GMT
  9.  
  10. silly c problem
  11.  
  12. Others already pointed out the problem with this code, but as a matter of
  13. style, why not use a switch? You wrote:
  14.  
  15. >#include <stdio.h>
  16. >
  17. >main()
  18. >{
  19. >  int num1, num2, res, ope, err;
  20. >
  21. >  printf("Please input two number!n");
  22. >  scanf("%d%d", &num1, &num2);
  23. >  printf("You entered %d and %d.nn", &num1, &num2);
  24. >  printf("And now the code for the operation!n");
  25. >  printf("1=Add, 2=Subtract, 3=Multiply, 4=Dividen");
  26. >  scanf("%d", &ope);
  27. >  printf("You entered %d.nn", &ope);
  28. >  err = 1;
  29. >  if(ope == 1)
  30. >    {
  31. >      res = num1 + num2;
  32. >      err = 0;
  33. >    }
  34. >  if(ope == 2)
  35. >    {
  36. >      res = num1 - num2;
  37. >      err = 0;
  38. >    }
  39. >  if(ope == 3)
  40. >    {
  41. >      res = num1 * num2;
  42. >      err = 0;
  43. >    }
  44. >  if(ope == 4)
  45. >    {
  46. >      res = num1 / num2;
  47. >      err = 0;
  48. >    }
  49. >  if(err == 1)
  50. >    printf("Wrong Code! Input only number 1 - 4!n");
  51. >  else
  52. >    printf("The result is %d %d %d %dn.", &num1, &num2, &res, &err);
  53. >}
  54.  
  55. whereas you could have written:
  56.  
  57. #include <stdio.h>
  58.  
  59. main()
  60. {    int num1, num2, res, ope, err;
  61.  
  62.     printf("Please input two number!\n");       // you probably meant \n?
  63.     scanf("%d%d", &num1, &num2);
  64.     printf("You entered %d and %d.\n\n", &num1, &num2);
  65.     printf("And now the code for the operation!\n");
  66.     printf("1=Add, 2=Subtract, 3=Multiply, 4=Dividen");
  67.     scanf("%d", &ope);
  68.     printf("You entered %d.\n\n", &ope);
  69.     err = 0;          // note how err is initialized to 0 rather than 1
  70.     switch (ope) {
  71.         case 1:
  72.             res = num1 + num2;
  73.             break;
  74.         case 2:
  75.             res = num1 - num2;
  76.             break;
  77.         case 3:
  78.             res = num1 * num2;
  79.             break;
  80.         case 4:
  81.             res = num1 / num2;      // be careful not to decide by 0
  82.             break;
  83.         default:
  84.             err = 1;
  85.     }
  86.  
  87.     if(err == 1)
  88.         printf("Wrong Code! Input only number 1 - 4!\n");
  89.     else
  90.         printf("The result is %d %d %d %dn.", &num1, &num2, &res, &err);
  91. }
  92.  
  93.  
  94. Bye,
  95.  
  96. Hans
  97.  
  98.